﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace WinHBM.Core.Net.Sockets.Cmd
{
    public class LogoutCmd : SocketCmd
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(LogoutCmd));

        public override void executeCmd(JObject request, FlashSocketServer socket)
        {
            FileStream fs = null;
            UserInfo user = null;
            try
            {
                //读取文件流对象 
                fs = new FileStream("user.bin", FileMode.OpenOrCreate);
                if (fs.Length > 0)
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    //读出存在user.bin 里的用户信息
                    user = bf.Deserialize(fs) as UserInfo;
                    user.auto = false;
                    bf.Serialize(fs,user);
                    //发送响应数据给Flash
                    socket.Send(wrapCmd(CMD_LOGOUT, new JObject()));
                }
                else
                {
                    socket.Send(wrapCmd(CMD_LOGOUT, "文件读取失败"));
                }
            }
            catch (Exception e)
            {
                socket.Send(wrapCmd(CMD_GETUSERINFO, e.Message));
                Logger.Error(e);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Flush();
                    fs.Close();
                }

            }
        }
    }
}
